<?php
require "indexModelclass.php";

$index = new indexAction;

class indexAction{

	public function index()
	{
		$nonce = $_GET['nonce'];
		$token = 'weixin';
		$tiemstamp = $_GET['tiemstamp'];
		$echostr = $_GET['echostr'];
		$signature = $_GET['signature'];
		$array = array($nonce,$tiemstamp,$token);
		sort($array);
		$str = sha1(implode($array));
		if($str==$signature && $echostr){          //微信在第一次绑定URL的时候会发送4个参数来验证你的URL地址,验证通过后,之后只会发送三个参数,所以可以用来户触发用户事件
			echo $echostr;
			exit;
		}else{
			$this->reponseMsg();
		}
	}




	public function reponseMsg()
	{

		//1.获取到微信推送过来的数据,格式如下
		$postArr = $GLOBALS['HTTP_RAW_POST_DATA'];
		/*
		<xml>
		<ToUserName><![CDATA[toUser]]></ToUserName>
		<FromUserName><![CDATA[FromUser]]></FromUserName>
		<CreateTime>123456789</CreateTime>
		<MsgType><![CDATA[event]]></MsgType>
		<Event><![CDATA[subscribe]]></Event>
		</xml>

*/
		$postObj = simplexml_load_string($postArr);    //把微信推送过来的XML格式转化成对象格式数据;
    	

    	if($postObj->MsgType =='event'){
		/*<xml>
		<ToUserName><![CDATA[toUser]]></ToUserName>
		<FromUserName><![CDATA[fromUser]]></FromUserName>
		<CreateTime>12345678</CreateTime>
		<MsgType><![CDATA[text]]></MsgType>
		<Content><![CDATA[你好]]></Content>
		</xml>*/

            // 如果是关注事件即刚关注微信公众号的时候
    		if($postObj->Event == 'subscribe'){

    			$toUser = $postObj->FromUserName;
    			$fromUser = $postObj->ToUserName;
    			$create = time();
    			$msgType = 'text';
    			$content = '欢迎观众我的微信测试号';
    			$template = "<xml>
							<ToUserName><![CDATA[%s]]></ToUserName>
							<FromUserName><![CDATA[%s]]></FromUserName>
							<CreateTime>%s</CreateTime>
							<MsgType><![CDATA[%s]]></MsgType>
							<Content><![CDATA[%s]]></Content>
							</xml>";

				$info = sprintf($template,$toUser,$fromUser,$create,$msgType,$content);
				echo $info;
    		}
    	}


    	if($postObj->MsgType =='text' && $postObj->Content =='你是猪'){

    		/*<xml>
			<ToUserName><![CDATA[toUser]]></ToUserName>
			<FromUserName><![CDATA[fromUser]]></FromUserName>
			<CreateTime>12345678</CreateTime>
			<MsgType><![CDATA[text]]></MsgType>
			<Content><![CDATA[你好]]></Content>
			</xml>*/
   			$toUser = $postObj->FromUserName;
			$fromUser = $postObj->ToUserName;
			$create = time();
			$msgType = 'text';
			$content = '你才是猪,SB';
			$template = "<xml>
						<ToUserName><![CDATA[%s]]></ToUserName>
						<FromUserName><![CDATA[%s]]></FromUserName>
						<CreateTime>%s</CreateTime>
						<MsgType><![CDATA[%s]]></MsgType>
						<Content><![CDATA[%s]]></Content>
						</xml>";
			$info = sprintf($template,$toUser,$fromUser,$create,$msgType,$content);
			echo $info;

    	}

    	if($postObj->MsgType=="text"){

    		switch($postObj->Content){
    			case 1:
    				$content = '战狼二最近很火1';
    			break;

    			case 2:
    				$content = '战狼二最近很火2';
    			break;
   
    			case 3:
    				$content = '战狼二最近很火3';
    			break;

    			case 4:
    				$content = '战狼二最近很火4';
    			break;

    			case $postObj->Content:
    				$content = $postObj->Content;
    			break;
    		}
	    		$toUser = $postObj->FromUserName;
				$fromUser = $postObj->ToUserName;
				$create = time();
				$msgType = 'text';
				$template = "<xml>
							<ToUserName><![CDATA[%s]]></ToUserName>
							<FromUserName><![CDATA[%s]]></FromUserName>
							<CreateTime>%s</CreateTime>
							<MsgType><![CDATA[%s]]></MsgType>
							<Content><![CDATA[%s]]></Content>
							</xml>";
				$info = sprintf($template,$toUser,$fromUser,$create,$msgType,$content);
				echo $info;
    		
    	}

	}

}

















$index ->index();